package com.neusoft.elmboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.utils.Constants;

@RestController
@RequestMapping("/GPT")
public class GPTController {

    @GetMapping("/GPTResponse")
    public String getGPTResponse(String prompt) throws Exception {
    	Constants.apiKey="sk-8081d969091c4de58303c3b9b6a59f54";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg = Message.builder().role(Role.SYSTEM.getValue()).content("你是一个名叫“饿了吧”外卖平台的人工智能助手小饿，在回答问题的时候一定不要长篇大论，要言简意赅，最好单次回答不超过50个字，回答要诙谐幽默").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(prompt).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        GenerationResult result = gen.call(param);
        
        // 提取content的值
        if (result.getOutput() != null && result.getOutput().getChoices() != null && !result.getOutput().getChoices().isEmpty()) {
            String content = result.getOutput().getChoices().get(0).getMessage().getContent();
            return content;
        } else {
            return "小饿不知道怎么回答你的问题呢，你可以换个问题问问小饿哦~";
        }
        
    }
}